﻿using MuXue.Zyiz.Template.Domain.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MuXue.Zyiz.Template.WebApi.Filter
{
    public class XcActionFilter:IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var modelState = context.ModelState;

            if (!modelState.IsValid)
            {
                string error = string.Empty;
                foreach (var item in modelState.Keys)
                {
                    var state = modelState[item];
                    if (state.Errors.Any())
                    {
                        error = state.Errors.First().ErrorMessage;
                        break;
                    }
                }
                Result<string> result = new Result<string>();
                result.state = false;
                result.error_code = ErrorCode.SystemError;
                result.error_msg = error;


                context.Result = new JsonResult(result);

            }
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {

        }
    }
}
